'''
SDL2 text provider
==================

Based on SDL2 + SDL2_ttf
'''

__all__ = ('LabelSDL2', )
from kivy.graphics.texture import Texture
from kivy.compat import PY2
from kivy.core.text import LabelBase
from kivy.core.text._text_sdl2 import (_SurfaceContainer, _get_extents,
                                       _get_fontdescent, _get_fontascent,
                                       fast_multi_lines_layout, fast_shorten_layout, fast_unrestricted_layout)

class LabelSDL2(LabelBase):

    def _get_font_id(self):
        if PY2:
            try:
                return '|'.join([unicode(self.options[x]) for x
                    in ('font_size', 'font_name_r', 'bold', 'italic')])
            except UnicodeDecodeError:
                pass
        return '|'.join([str(self.options[x]) for x
            in ('font_size', 'font_name_r', 'bold', 'italic')])

    def get_extents(self, text):
        try:
            if PY2:
                text = text.encode('UTF-8')
        except:
            pass
        return _get_extents(self, text)

    def get_descent(self):
        return _get_fontdescent(self)

    def get_ascent(self):
        return _get_fontascent(self)

    def _render_begin(self):
        self._surface = _SurfaceContainer(self._size[0], self._size[1])

    def _render_text(self, text, x, y):
        self._surface.render(self, text, x, y)

    def _render_end(self):
        return self._surface.get_data()

    def _fast_layout_text(self):
        return w, h

    def _sdl_texture_fill(self, texture):
        if self.data:
            self.texture.blit_data(self.data)

    def fast_render(self, character='z'):
        self.resolve_font_name()
        try:
            self._text = self._text.rstrip()
        except AttributeError:
            self._text = str(self._text)

        text_size = self.options.get('text_size', (None, None))
        if text_size[0] is not None:
            line_height = self.options.get('line_height', 1.0)
        shorten =  self.options.get('shorten', False)
        if text_size[0] is None:
            self.data = fast_unrestricted_layout(self, self._text)
        elif shorten:
            self.data = fast_shorten_layout(self, self._text, character, text_size[0])
        else:
            self.data = fast_multi_lines_layout(self, self._text, character, text_size[0], line_height)

        # if no text are rendered, return nothing.
        if self.data is None:
            self.texture = self.texture_1px
            return
        self._size_texture = self.data.size
        self._size = (self.data.size[0], self.data.size[1])
        width, height = self.data.size
        if width <= 1 or height <= 1:
            self.texture = self.texture_1px
            return
        texture = self.texture
        if texture is None or \
                width != texture.width or \
                height != texture.height:
            texture = Texture.create(size=(width, height),
                                     mipmap=self.options['mipmap'],
                                     callback=self._sdl_texture_fill)
            texture.flip_vertical()
            texture.add_reload_observer(self._sdl_texture_fill)
            self.texture = texture
        else:
            texture.ask_update(self._sdl_texture_fill)

